document.addEventListener('DOMContentLoaded', function() {
    // 获取当前时间
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');

    // 格式化当前日期
    const formattedDate = `${year}-${month}-${day}`;

    // 设置下欠时间的默认值
    document.getElementById('debt-date').value = formattedDate;

    // 设置还款时间的默认值为当前日期之后的一天
    const paymentDate = new Date(now);
    paymentDate.setDate(paymentDate.getDate() + 1); // 增加一天
    const paymentMonth = String(paymentDate.getMonth() + 1).padStart(2, '0');
    const paymentDay = String(paymentDate.getDate()).padStart(2, '0');
    const formattedPaymentDate = `${year}-${paymentMonth}-${paymentDay}`;

    document.getElementById('payment-date').value = formattedPaymentDate;

    // 添加事件监听器，确保下欠时间不晚于还款时间
    document.getElementById('debt-date').addEventListener('change', function() {
        const debtDate = new Date(this.value);
        const paymentDate = new Date(document.getElementById('payment-date').value);

        if (debtDate >= paymentDate) {
            this.value = formattedDate; // 重置为当前日期
        }
    });

    document.getElementById('payment-date').addEventListener('change', function() {
        const paymentDate = new Date(this.value);
        const debtDate = new Date(document.getElementById('debt-date').value);

        if (paymentDate <= debtDate) {
            this.value = formattedPaymentDate; // 重置为当前日期之后的一天
        }
    });
});